package com.macro.mall.boot.wechatpay.config;

import com.github.binarywang.wxpay.config.WxPayConfig;
import com.github.binarywang.wxpay.service.WxPayService;
import com.github.binarywang.wxpay.service.impl.WxPayServiceImpl;
import com.macro.mall.boot.wechatpay.props.WxPayProperties;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.util.Assert;

import java.util.HashMap;
import java.util.Map;

/**
 * 微信支付配置
 *
 * @author xuyanjun
 * @date 2019-07-10
 */
@Slf4j
@Configuration
public class WxPayConfiguration {

    /**
     * 微信支付服务MAP<BusinessAppId,WxPayService></>
     */
    private static Map<Integer, WxPayService> WX_PAY_SERVICE_MAP = new HashMap<>();

    /**
     * 获取微信支付服务
     *
     * @param businessAppId 业务AppId
     * @return 微信支付服务
     */
    public static WxPayService getWxPayServiceByBusinessAppId(int businessAppId) {
        Assert.isTrue(businessAppId > 0, "业务AppId不能为空。");

        WxPayService wxPayService = WX_PAY_SERVICE_MAP.get(businessAppId);

        if (wxPayService == null) {
            throw new IllegalStateException("未找到响应的微信支付配置:" + businessAppId);
        }

        return wxPayService;
    }

    /**
     * 获取默认微信支付服务(千猪小程序2)
     *
     * @param businessAppId 业务AppId
     * @return 微信支付服务
     */
    public static WxPayService getDefaultWxPayService() {
        WxPayService wxPayService = WX_PAY_SERVICE_MAP.get(10001);

        if (wxPayService == null) {
            throw new IllegalStateException("未找到响应的微信支付配置:" + 10001);
        }

        return wxPayService;
    }

    @Profile(value = {"dev", "test"})
    @Bean
    public Inner wxPayPropertiesDev() {
        // 美逛严选(10001)
        WxPayConfig wxPayConfig10001 = new WxPayConfig();
        wxPayConfig10001.setAppId("wx8623ac1cbaaf7a3d");
        wxPayConfig10001.setMchId("1569228331");
        wxPayConfig10001.setMchKey("kijmhgtyhbngtresdkolkiuyjhujnvfg");
        wxPayConfig10001.setKeyPath("classpath:KEY-PATH/apiclient_cert.p12");

        WxPayService wxPayService10001 = new WxPayServiceImpl();
        wxPayService10001.setConfig(wxPayConfig10001);

        WX_PAY_SERVICE_MAP.put(10001, wxPayService10001);

        return new Inner();
    }

    @Profile(value = {"prod"})
    @Bean
    public Inner wxPayPropertiesPro() {
        // 千猪小程序(10001)
        WxPayConfig wxPayConfig10001 = new WxPayConfig();
        wxPayConfig10001.setAppId("wx8623ac1cbaaf7a3d");
        wxPayConfig10001.setMchId("1569228331");
        wxPayConfig10001.setMchKey("kijmhgtyhbngtresdkolkiuyjhujnvfg");
        wxPayConfig10001.setKeyPath("classpath:KEY-PATH/apiclient_cert.p12");

        WxPayService wxPayService10001 = new WxPayServiceImpl();
        wxPayService10001.setConfig(wxPayConfig10001);

        WX_PAY_SERVICE_MAP.put(10001, wxPayService10001);

        return new Inner();
    }

    @Profile(value = {"union"})
    @Bean
    public Inner wxPayPropertiesUnion() {
        WxPayProperties wxPayProperties = new WxPayProperties();
        wxPayProperties.setAppId("wx9fa52448dda068e2")
                .setMchId("1555273021")
                .setMchKey("meiguangyoupinlovemeiguangyoupin")
                .setKeyPath("classpath:KEY-PATH/apiclient_cert.p12");

        return new Inner();
    }
    private static class Inner {

    }
}
